<template>
  <div class="app-container">
    <el-page-header @back="goBack" v-if="!flag"> </el-page-header>
    <el-form
      v-if="flag"
      :model="queryParams"
      ref="queryRef"
      label-width="auto"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="日期" style="width: 308px">
        <el-date-picker
          v-model="dateRange"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="报工类型" prop="type">
        <el-select
          v-model="queryParams.type"
          clearable
          style="width: 180px"
          placeholder="请选择"
          @change="changeReportType"
        >
          <el-option
            v-for="item in reportType"
            :key="item.label"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="类别" prop="machineTypeName" v-if="typeFlag">
        <el-select
          v-model="queryParams.machineTypeName"
          clearable
          style="width: 180px"
          placeholder="请选择"
        >
          <el-option value="骑订" label="骑订"></el-option>
          <el-option value="胶订" label="胶订"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="车间" prop="workshopName">
        <el-select style="width: 180px" v-model="queryParams.workshopName">
          <el-option label="一车间" value="一车间"></el-option>
          <el-option label="二车间" value="二车间"></el-option>
          <el-option label="三车间" value="三车间"></el-option>
          <el-option label="四车间" value="四车间"></el-option>
          <el-option label="五车间" value="五车间"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="机台" prop="machineName">
        <el-input
          v-model.trim="queryParams.machineName"
          style="width: 180px"
          placeholder="请输入机台"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          clearable
          style="width: 180px"
          placeholder="请选择"
        >
          <el-option
            v-for="item in report_status"
            :key="item.label"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="生产单编码" prop="produceNo">
        <el-input
          v-model.trim="queryParams.produceNo"
          style="width: 180px"
          placeholder="请输入生产单编码"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报工单编码" prop="code">
        <el-input
          v-model.trim="queryParams.code"
          style="width: 180px"
          placeholder="请输入报工单编码"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上报人" prop="createBy">
        <el-input
          v-model.trim="queryParams.createBy"
          style="width: 180px"
          placeholder="请输入上报人"
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="报工日期" prop="searchDate">
        <el-date-picker
          style="width: 180px"
          v-model.trim="queryParams.searchDate"
          value-format="YYYY-MM-DD"
          type="date"
          placeholder="请选择日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Download" @click="handleExport"
          >导出</el-button
        >
      </el-col>
    </el-row>
    <div>
      <el-table
        v-loading="loading"
        border
        :data="dataList"
        height="65vh"
        :summary-method="getSummaries"
        show-summary
      >
        <el-table-column label="序号" width="60" type="index" align="center" />
        <el-table-column
          label="状态"
          min-width="80"
          align="center"
          :show-overflow-tooltip="true"
          prop="status"
        >
          <template v-slot="{ row }">
            <dict-tag :options="report_status" :value="row.status" />
          </template>
        </el-table-column>
        <el-table-column
          label="报工单编码"
          min-width="200"
          align="center"
          :show-overflow-tooltip="true"
          prop="code"
        >
          <template v-slot="{ row }">
            <el-button type="primary" @click="handleView(row)" text>{{
              row.code
            }}</el-button>
          </template>
        </el-table-column>
        <el-table-column
          label="生产单编码"
          min-width="140"
          align="center"
          :show-overflow-tooltip="true"
          prop="produceCode"
        >
          <template v-slot="{ row }">
            <el-button
              type="primary"
              @click="handleViewProduce(row, index)"
              text
              >{{ row.produceCode }}</el-button
            >
          </template>
        </el-table-column>
        <el-table-column
          label="员工名称"
          width="100"
          align="center"
          :show-overflow-tooltip="true"
          prop="userName"
        >
        </el-table-column>
        <el-table-column
          label="出版号"
          width="100"
          align="center"
          :show-overflow-tooltip="true"
          prop="pubNo"
        >
        </el-table-column>

        <el-table-column
          label="车间"
          align="center"
          width="100"
          :show-overflow-tooltip="true"
          prop="workshopName"
        >
        </el-table-column>
        <el-table-column
          label="机台"
          align="center"
          min-width="220"
          :show-overflow-tooltip="true"
          prop="machineName"
        >
        </el-table-column>
        <el-table-column
          label="报工日期"
          width="120"
          align="center"
          :show-overflow-tooltip="true"
          prop="reportDate"
        >
          <template v-slot="{ row }">
            {{ row.reportDate ? row.reportDate.split(" ")[0] : "" }}
          </template>
        </el-table-column>
        <el-table-column
          label="上报人"
          min-width="100"
          align="center"
          :show-overflow-tooltip="true"
          prop="createBy"
        >
        </el-table-column>
        <el-table-column
          label="品种"
          min-width="100"
          prop="breed"
          align="center"
        ></el-table-column>
        <el-table-column
          label="书刊名称"
          min-width="240"
          prop="orderName"
          align="center"
        ></el-table-column>
        <el-table-column
          label="印张"
          min-width="60"
          align="center"
          :show-overflow-tooltip="true"
          prop="sheet"
        ></el-table-column>
        <el-table-column
          label="贴标"
          min-width="60"
          align="center"
          :show-overflow-tooltip="true"
          prop="labeling"
        ></el-table-column>
        <el-table-column
          label="印数"
          min-width="60"
          align="center"
          prop="printAmount"
        ></el-table-column>
        <el-table-column
          label="完成数量"
          min-width="100"
          align="center"
          prop="finishNum"
        ></el-table-column>
        <el-table-column
          label="用纸令数"
          width="100"
          align="center"
          prop="usePaper"
        ></el-table-column>
        <el-table-column
          label="计件令数"
          align="center"
          prop="pieceLing"
        ></el-table-column>
        <el-table-column
          label="计件标准"
          width="80"
          align="center"
          :show-overflow-tooltip="true"
          prop="standard"
        ></el-table-column>
        <el-table-column
          label="计件金额"
          align="center"
          :show-overflow-tooltip="true"
          prop="amount"
        ></el-table-column>
        <el-table-column
          label="印色"
          align="center"
          :show-overflow-tooltip="true"
          prop="printColor"
        >
          <template #default="scope">
            <dict-tag
              :options="ems_order_print_color"
              :value="scope.row.printColor"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="换版数量"
          width="80"
          align="center"
          :show-overflow-tooltip="true"
          prop="exchangeNum"
        ></el-table-column>
        <el-table-column
          label="计件标准(换版)"
          width="80"
          align="center"
          prop="standardH"
        ></el-table-column>
        <el-table-column
          label="计件金额(换版)"
          align="center"
          :show-overflow-tooltip="true"
          prop="amountH"
        ></el-table-column>
        <el-table-column
          label="预计用纸令数"
          width="120"
          align="center"
          prop="expectLing"
        >
        </el-table-column>
        <el-table-column
          label="实际用纸令数"
          min-width="100"
          align="center"
          prop="realityLing"
        ></el-table-column>
        <el-table-column
          label="盈亏令数"
          min-width="100"
          align="center"
          prop="ykLing"
        ></el-table-column>
        <el-table-column
          label="另行加放"
          min-width="100"
          align="center"
          prop="lxjf"
        ></el-table-column>
        <el-table-column
          label="吨位系数"
          align="center"
          prop="tonnageCoefficient"
        ></el-table-column>
        <el-table-column
          label="损耗比例"
          align="center"
          prop="lossCoefficient"
        ></el-table-column>
        <el-table-column
          label="备注"
          min-width="120"
          align="center"
          :show-overflow-tooltip="true"
          prop="remark"
        >
        </el-table-column>
        <el-table-column
          label="创建人"
          min-width="120"
          align="center"
          :show-overflow-tooltip="true"
          prop="createBy"
        >
        </el-table-column>
        <el-table-column
          fixed="right"
          label="创建时间"
          width="100"
          align="center"
          prop="createTime"
        >
          <template v-slot="{ row }">
            {{ parseTime(row.createTime) }}
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>
  
  <script setup name="SearchReportDetail">
import { downLoadBlob } from "@/utils/ruoyi";
import { getMesWorkShop } from "@/api/basicInfo/workshop.js";
import {
  getReportDetail,
  exportReportDetail,
} from "@/api/production/printReport.js";
import useTagsViewStore from "@/store/modules/tagsView";
const { proxy } = getCurrentInstance();
const loading = ref(true);
const dataList = ref([]);
const showSearch = ref(true);
const total = ref(0);
const router = useRouter();
const route = useRoute();
const workshopOptions = ref([]);
const flag = ref(true);
const tagsViewStore = useTagsViewStore();
const typeFlag = ref(false);
const finishNumSumT = ref(0);
const usePaperSumT = ref(0);

const reportType = ref([
  { label: "印刷", value: "1" },
  { label: "折页", value: "2" },
  { label: "裁切", value: "3" },
  { label: "覆膜", value: "4" },
  { label: "装订", value: "5" },
  // 1印刷报工 2折页报工 3裁切报工 4覆膜报工 5装订报工 6其他报工
]);

// 列显隐信息
const columns = ref([
  { key: 0, label: `状态`, visible: true },
  { key: 1, label: `报工单编码`, visible: true },
  { key: 2, label: `生产单编码`, visible: true },
  { key: 3, label: `员工名称`, visible: true },
  { key: 4, label: `出版号`, visible: true },
  { key: 5, label: `机台`, visible: true },
  { key: 6, label: `报工日期`, visible: true },
  { key: 7, label: `上报人`, visible: true },
  { key: 8, label: `品种`, visible: true },
  { key: 9, label: `书刊名称`, visible: true },
  { key: 10, label: `印张`, visible: true },
  { key: 11, label: `贴标`, visible: true },
  { key: 12, label: `印数`, visible: true },
  { key: 13, label: `完成数量`, visible: true },
  { key: 14, label: `计件令数`, visible: true },
  { key: 15, label: `计件金额`, visible: true },
  { key: 16, label: `印色`, visible: true },
  { key: 17, label: `换版数量`, visible: true },
  { key: 18, label: `计件标准（换版）`, visible: true },
  { key: 19, label: `计件金额（换版`, visible: true },
  { key: 20, label: `预计用纸令数`, visible: true },
  { key: 21, label: `实际用纸令数`, visible: true },
  { key: 22, label: `盈亏令数`, visible: true },
  { key: 23, label: `另行加放）`, visible: true },
  { key: 24, label: `损耗比例`, visible: true },
  { key: 25, label: `备注`, visible: true },
]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 50,
    allotNum: undefined,
    allotUnitid: undefined,
    allotDate: undefined,
    type: "1",
    status: "1",
  },
});

const getDateNow = () => {
  var myDate = new Date();
  var year = myDate.getFullYear();
  var month = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月，所以要加1);
  var day = myDate.getDate(); //获取当前日（1-31）
  if (month < 10) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  var firstDay = year + "-" + month + "-01";
  return firstDay;
};
const getCurrentDay = () => {
  var now = new Date();
  var year = now.getFullYear();
  var month = now.getMonth() + 1; // 月份从0开始，需要加1
  var day = now.getDate();
  if (month < 10) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  const currentDay = year + "-" + month + "-" + day;
  return currentDay;
};
const dateRange = ref([getDateNow(), getCurrentDay()]);

const { queryParams } = toRefs(data);
const { ems_production_audit, report_status, ems_order_print_color } =
  proxy.useDict(
    "ems_production_audit",
    "report_status",
    "ems_order_print_color"
  );

const getSummaries = (param) => {
  const { columns, data } = param;
  const sums = [];
  columns.forEach((column, index) => {
    if (index === 0) {
      sums[index] = "合计";
      return;
    }

    switch (column.property) {
      case "finishNum":
        sums[index] = finishNumSumT.value;
        break;
      case "usePaper":
        sums[index] = usePaperSumT.value;
        break;
      default:
        break;
    }
  });
  return sums;
};

const changeReportType = (row) => {
  if (row == 5) {
    //装订
    typeFlag.value = true;
  } else {
    typeFlag.value = false;
  }
  searchType.value = row;
};

const getList = () => {
  loading.value = true;
  //breed=目录&type=1&produceNo=202406-0323&status=1&info=true
  if (route.query?.breed) {
    queryParams.value.breed = route.query.breed;
    queryParams.value.produceNo = route.query.produceNo;
    queryParams.value.type = route.query.type;
    queryParams.value.status = route.query.status;
    flag.value = false;
  } else {
    queryParams.value.startDateTime = Array.isArray(dateRange.value)
      ? dateRange.value[0]
      : undefined;
    queryParams.value.endDateTime =
      Array.isArray(dateRange.value) > 0 ? dateRange.value[1] : undefined;
  }
  getReportDetail(queryParams.value)
    .then((res) => {
      loading.value = false;
      dataList.value = res.rows;
      if (res.rows.length > 0) {
        finishNumSumT.value = res.rows[0].finishNumSum;
        usePaperSumT.value = res.rows[0].usePaperSum;
      }
      total.value = res.total;
    })
    .catch((err) => {
      loading.value = false;
    });
};

// 查看
const handleViewProduce = (row, index) => {
  const produceId = row.produceId;
  if (row.produceCode.includes("FM")) {
    router.push({
      path: "/production/cover/coverDetails",
      query: {
        id: produceId,
        info: true,
        moduleCode: "searchReportDetail",
      },
    });
  } else {
    router.push({
      path: "/production/text/textDetails",
      query: {
        id: produceId,
        info: true,
        moduleCode: "searchReportDetail",
      },
    });
  }
};

// 查看
const handleView = (row) => {
  if (row.type == "1") {
    router.push({
      //印刷报工
      path: "/production/printReport/printReportDetails",
      query: {
        id: row.reportId,
        info: true,
      },
    });
  } else if (row.type == "2") {
    //折页报工
    router.push({
      path: "/production/foldingReport/foldReportDetails",
      query: {
        id: row.reportId,
        info: true,
      },
    });
  } else if (row.type == "3") {
    //裁切报工
    router.push({
      path: "/production/cuttingReport/detail",
      query: {
        id: row.reportId,
        info: true,
      },
    });
  } else if (row.type == "4") {
    //覆膜报工
    router.push({
      path: "/production/coverReport/coverReportDetails",
      query: {
        id: row.reportId,
        info: true,
      },
    });
  } else if (row.type == "5") {
    //装订报工
    router.push({
      path: "/production/bindReport/bindReportDetails",
      query: {
        id: row.reportId,
        info: true,
      },
    });
  }
};
// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
};

// 导出
const handleExport = () => {
  exportReportDetail(queryParams.value).then((res) => {
    downLoadBlob(res, "报工明细");
  });
};

//  返回
const goBack = () => {
  // router.replace({ path: "/production/orderProgress" });
  setTimeout(() => {
    router.go(-1);
    tagsViewStore.delView(route);
  }, 0);
};

// const getWorkShopList = async () => {
//   const res = await getMesWorkShop({ ...queryParams.value });
//   workshopOptions.value = [...res.rows];
// };
// getWorkShopList();

getList();
</script>
  
  <style lang="scss" scoped></style>
  